<template>
  <div style="padding-left: 100px">
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="80px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:70%" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:70%" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <!-- 日期组件 -->
        <el-date-picker v-model="formData.timeOfEntry" style="width:70%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="应聘形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:70%" placeholder="请选择应聘形式">
          <el-option v-for="item in EmpType" :key="item.id" :label="item.label" :value="item.id" />
          <!-- <el-option label="正式" value="1" />
          <el-option label="非正式" value="2" /> -->
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:70%" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:70%" @focus="hFocus" />
        <div v-show="isLoad">
          <el-tree :data="employee" :props="{label:'name'}" accordion @node-click="handleNodeClick" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:70%" placeholder="请选择转正时间" />
      </el-form-item>
      <el-form-item>
        <el-button @click="$refs.addForm.resetFields()">重 置</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { TYPE_MAP } from '@/constant/index'
import { arrayToTree } from '@/utils'
import { getDepartments } from '@/api/department'
import { addEmployee } from '@/api/employees'
export default {
  data() {
    return {
      EmpType: Object.keys(TYPE_MAP).map(key => { return { id: key, label: TYPE_MAP[key] } }),
      formData: {
        username: '', // 用户名
        mobile: '13579246810', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      employee: [],
      isLoad: false
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$refs.addForm.resetFields()
    },
    // 树形节点点击触发
    handleNodeClick(data, node) {
      console.log(data, node)
      if (data.children.length) {
        // 有子级
        return
      }
      this.formData.departmentName = data.name
      this.isLoad = false
    },
    hFocus() {
      this.loadDepartments()
    },
    // 请求部门列表
    async loadDepartments() {
      const { data: res } = await getDepartments()
      this.isLoad = true
      // console.log(res)
      // 把数组第一元素删除，剩余存到list
      res.depts.shift()
      // 数组转树
      this.employee = arrayToTree(res.depts)
    },
    async doAdd() {
      try {
        await addEmployee(this.formData)
        this.$message.success('添加员工成功')
        // 通知父组件
        this.$emit('success')
      } catch (e) {
        this.$message.error(e)
      }
    },
    onSubmit() {
      // 手动兜底校验
      this.$refs.addForm.validate(valid => {
        if (!valid) return
        console.log('ok')
        this.doAdd()
      })
    }
  }
}
</script>

<style>

</style>
